/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.cs.piit.hibernate.GenericDAO;

import ru.vsu.cs.piit.hibernate.HibernateSessionFactory;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;

/**
 *
 * @author Галчонок
 */
public abstract class GenericHibernateDAO<T, ID extends Serializable>
{

    private Class<T> persistentClass;

    public GenericHibernateDAO() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
     }


    protected static Session getSession() {
       return HibernateSessionFactory.getSession();
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    @SuppressWarnings("unchecked")
    public T findById(ID id, boolean lock) {
        T entity;
        if (lock)
            entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
        else
            entity = (T) getSession().load(getPersistentClass(), id);

        return entity;
    }

    @SuppressWarnings("unchecked")
    public List<T> findAll() {
//        Criteria criteria = getSession().createCriteria(T.class);
//        criteria.
        return findByCriteria();
    }

    @SuppressWarnings("unchecked")
    public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        if (exampleInstance != null) {
	        Example example =  Example.create(exampleInstance);
	        if (excludeProperty != null) {
		        for (String exclude : excludeProperty) {
		            example.excludeProperty(exclude);
		        }
	        }
	        crit.add(example);
        }
        return crit.list();

    }

    @SuppressWarnings("unchecked")
    public static <T> T save(T entity) {
        Session s = getSession();
        s.getTransaction().begin();
        s.saveOrUpdate(entity);
        s.getTransaction().commit();
        return entity;
    }

    public static <T> void delete(T entity) {
        getSession().getTransaction().begin();
        getSession().delete(entity);
        getSession().getTransaction().commit();
    }

    public static <T> void flush() {
        getSession().flush();
    }

    public static <T> void clear() {
        getSession().clear();
    }

    /**
     * Use this inside subclasses as a convenience method.
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
   }

    public static <T> List<T> findAll(Class T)
    {
        return HibernateSessionFactory.getSession().createCriteria(T).list();
    }
}

